var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
function simpleEncode(values,maxValue) {

var chartData = ['s:'];
  for (var i = 0; i < values.length; i++) {
    var currentValue = values[i];
    if (!isNaN(currentValue) && currentValue >= 0) {
    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
    }
      else {
      chartData.push('_');
      }
  }
return chartData.join('');
}


function draw(tableid, parentEl, balans, income){
  var chartBaseUrl = "http://chart.apis.google.com/chart?";
  var chartParams = "chs=450x200&cht=p";

  var values = "&chd=";
  var labels = "&chl=";
  var colours = "&chco=df4444,ee9f00,8d86dc"
  var valueArray = new Array();
  var max = 1;

  var tbl = document.getElementById(tableid);
  // alert (tbl.innerHTML);
  var rows = tbl.getElementsByTagName("tr");   
  // the first row is going to have the headings
//  var graphedPer = headings[0].innerHTML;
//  var graphLabel = headings[1].innerHTML;
//  document.write("<P>" + graphLabel + " per " + graphedPer);
  for(i = 1; i < rows.length; i++){
    //alert (rows[i].innerHTML);
    var cols = rows[i].getElementsByTagName("td");
    if (i == 1) {
      labels = labels + cols[0].innerHTML;
    } else {
      labels = labels + "|" + cols[0].innerHTML;
    }
    valueArray[i - 1] = cols[1].innerHTML;
    if (parseFloat(cols[1].innerHTML) > parseFloat(max)) {
      max = cols[1].innerHTML;
    }

  }
  
  values = values + simpleEncode(valueArray,max);
//  alert(chartBaseUrl + chartParams + labels + values);
  var gomArray = new Array();
  if (parseFloat(balans) < 0){
	  gomArray[0] = 0;
  } else {
	  gomArray[0] = balans;
  }
  
  document.getElementById(parentEl).innerHTML = "<P><img src=" + chartBaseUrl + chartParams +colours+ labels + values + ">" +
  		"<img src=" + chartBaseUrl + "chs=200x200&cht=gom" + "&chl=Баланс" +"&chd="+ simpleEncode(gomArray,income) + "></P>";
}